package com.wangjiyuan.shiwupai.ui_food.food_class;

import android.util.Log;

import com.wangjiyuan.shiwupai.bean.first_food.FoodClassBean;
import com.wangjiyuan.shiwupai.bean.first_food.FoodSortBean;

import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by QXL on 2016/11/30.
 */

public class FoodClassPresenter implements FoodClassContract.IFoodClassPresenter {
    private FoodClassContract.IFoodClassModel iFoodClassModel;
    private FoodClassContract.IFoodClassView iFoodClassView;

    private Subscription subscriptionFoodClass,subscriptionSort;
    private Observable<FoodClassBean> foodClassObservable;

    public FoodClassPresenter (FoodClassActivity activity){
        iFoodClassView=activity;
        iFoodClassModel=new FoodClassModel();
    }
//    食物百科分类列表
    @Override
    public void getFoodClassObservable(Map<String, String> map) {
        foodClassObservable = iFoodClassModel.getFoodClassObservable(map);
        subscriptionFoodClass =foodClassObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FoodClassBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.e("Tag","FoodClass start");
                        iFoodClassView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        Log.e("Tag","FoodClass completed");
                        iFoodClassView.hideProgress();
                        subscriptionFoodClass.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("Tag","FoodClass error");
                        iFoodClassView.showProgress();
                        iFoodClassView.hideProgress();
                        subscriptionFoodClass.unsubscribe();
                    }

                    @Override
                    public void onNext(FoodClassBean foodClassBean) {
                        Log.e("Tag","FoodClass next");
                        iFoodClassView.loadFoodClassData(foodClassBean);
                    }
                });
    }

    @Override
    public void getNextFoodClassObservable(Map<String, String> map) {
        foodClassObservable = iFoodClassModel.getFoodClassObservable(map);
        subscriptionFoodClass =foodClassObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FoodClassBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.e("Tag","NextFoodClass start");
                        iFoodClassView.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        Log.e("Tag","NextFoodClass completed");
                        iFoodClassView.hideProgress();
                        subscriptionFoodClass.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("Tag","NextFoodClass error");
                        iFoodClassView.showProgress();
                        iFoodClassView.hideProgress();
                        subscriptionFoodClass.unsubscribe();
                    }

                    @Override
                    public void onNext(FoodClassBean foodClassBean) {
                        Log.e("Tag","NextFoodClass next");
                        iFoodClassView.loadNextFoodClassData(foodClassBean);
                    }
                });
    }

    //    营养素排序
    @Override
    public void getSortObservable( Map<String, String> map) {
        Observable<FoodSortBean> sortObservable = iFoodClassModel.getSortObservable( map);
        subscriptionSort=sortObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FoodSortBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.e("Tag","sort start");
                    }

                    @Override
                    public void onCompleted() {
                        Log.e("Tag","sort completed");
                        subscriptionSort.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("Tag","sort error");
                        subscriptionSort.unsubscribe();
                    }

                    @Override
                    public void onNext(FoodSortBean foodSortBean) {
                        Log.e("Tag","sort next");
                        iFoodClassView.loadSort(foodSortBean );
                        int size = foodSortBean.getTypes().size();
                        Log.e("Tag","Sort size==="+size);
                    }
                });
    }
}
